package TCP.Demo1;

import java.io.*;

import java.net.*;



public class TCPServer {

    private static final int PORT = 9876;



    public static void main(String[] args) {

        try (ServerSocket serverSocket = new ServerSocket(PORT)) {

            System.out.println("服务器已启动，监听端口: " + PORT);



            while (true) {

                try (Socket clientSocket = serverSocket.accept();

                     BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))) {



                    System.out.println("客户端已连接: " + clientSocket.getInetAddress());



                    String inputLine;

                    while ((inputLine = in.readLine()) != null) {

                        System.out.println("收到客户端消息: " + inputLine);

                    }



                } catch (IOException e) {

                    System.out.println("客户端断开连接。");

                }

            }

        } catch (IOException e) {

            System.err.println("无法启动服务器: " + e.getMessage());

        }

    }

}
